package com.wn.controller;

import com.auth0.jwt.interfaces.Claim;
import com.wn.annotation.RequireRoles;
import com.wn.entity.Emp;
import com.wn.entity.PageParam;
import com.wn.entity.Role;
import com.wn.entity.Userinfo;
import com.wn.service.EmpService;
import com.wn.utils.JwtUtil;
import com.wn.utils.Rsp;
import com.wn.utils.SystemControllerLog;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * (Emp)表控制层
 *
 * @author makejava
 * @since 2020-10-26 09:29:19
 */
@RestController
@RequestMapping("emp")
public class EmpController {
    /**
     * 服务对象
     */
    @Resource
    private EmpService empService;

    /**
     * 通过主键查询单条数据
     *
     * @param token 令牌
     * @return 单条数据
     */
    @GetMapping("getUserInfo")
    public Rsp getUserInfo(String token){
        String userId = JwtUtil.parseToken(token).get("id").asString();
        Emp emp = empService.queryById(userId);
        Role role = empService.selectRole(emp.getRoleid().toString());
        emp.setRoleSign(role.getRoleSign());
        return Rsp.success(emp);
    }

    /**
     * 登录
     * @param loginEmp
     * @return
     * @throws Exception
     */
    @PostMapping("/loginWithUsernameAndPass")
    public Rsp empLogin(@RequestBody Emp loginEmp) throws Exception {

        System.out.println(loginEmp);
        return empService.empLogin(loginEmp);

    }


    /**
     * 注销
     * @return
     */
    @PostMapping("/logout")
    public Rsp logout(){

        return Rsp.success("登出成功");
    }

    @SystemControllerLog(description = "分页查询员工信息")
    @PostMapping("/searchSomeNewsorderByPage")
    @RequireRoles(roles = {"admin", "super_admin"}, index = 1)
    @ApiOperation("查询所有 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<Emp> orderPageParam
            ,@RequestHeader("token") String token) {

//        log.info("userId:{}", userId);
//        log.info("roleId:{}", roleSign);

        return Rsp.success(empService.searchSomeByPage(orderPageParam));
    }

    //管理员根据id删除员工信息
    @SystemControllerLog(description = "根据id删除员工信息")
    @RequireRoles(roles = {"super_admin"},index = 1)
    @PostMapping("/deleteOneNewsorderTrue")
    @ApiOperation("通过ID物理删除")
    public Rsp deleteOneNewscategoryTrue(@RequestBody Emp newsorder,@RequestHeader("token") String token){
//        newsorder.setUpdateuserid("1");
        return empService.deleteOneTrue(newsorder.getId());
    }

    //根据id查询信息
    @PostMapping("/empSelectInfo")
    @ApiOperation("通过ID查询个人信息")
    public Emp empSelectInfo(@RequestBody Emp newsorder, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){
        Emp emp = empService.empSelectInfo(newsorder.getId());
        return emp;
    }

    @RequireRoles(roles = {"super_admin","admin"},index = 1)
    @PostMapping("/updateEmp")
    @ApiOperation("动态修改")
    public Rsp updateNewscategory(@RequestBody Emp newsorder,@RequestHeader("token") String token){


//        newsorder.setUpdateuserid("1");
        return empService.updateId(newsorder);
    }

    @SystemControllerLog(description = "添加单个员工信息")
    @RequireRoles(roles = {"super_admin","admin"},index = 1)
    @PostMapping("/addOneEmp")
    @ApiOperation("添加单个")
    public Rsp addOne(@RequestBody @Valid Emp newsorder,@RequestHeader("token") String token){

//        newsorder.setCreateuserid(userId);
        return empService.addOne(newsorder);
    }

}
